package com.nettest.action.usermanage;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.nettest.action.BaseJsonAction;
import com.nettest.bean.usermanage.DemoCriteriaVo;
import com.nettest.bean.usermanage.UserCriteriaVo;
import com.nettest.entity.demo.Grid50000;
import com.nettest.service.demo.Grid50000Service;

@Results({
		@Result(name = "input", type = "json", params = { "ignoreHierarchy",
				"false", "excludeNullProperties", "true", "excludeProperties",
				"drid50000Service,ids" }),
		@Result(name = "success", location = "/usermanage/createUser.jsp") })
public class UserManageAction extends BaseJsonAction {

	private static final long serialVersionUID = 7796054923782630546L;
	private DemoCriteriaVo criteria;

	private Grid50000 data;
	
	private List<Grid50000> lists;
	@Autowired
	private Grid50000Service drid50000Service;
	
	private String ids;
	public UserManageAction() {
		super();
	}

	@SuppressWarnings("unchecked")
	@Action(value = "list", results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "criteria" }) })
	public String list() {
		int intPage = getPage();
		int intRows = getRows();
		int firstResult = intPage * intRows - intRows;
			
		criteria.setPageSize(firstResult);
		criteria.setSortField(getSort());
		criteria.setDescOrAsc(getOrder());
		try {
			this.lists = (List<Grid50000>)drid50000Service.queryByCriteria(criteria);
			setTotal(drid50000Service.totalSize(criteria));
			setSuccess(true);
		} catch (Exception e) {
			setSuccess(false);
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "criteria" }) })
	public String save() {
		try {
			if(data.getId() != null) {
				//update case
				Grid50000 updateData = drid50000Service.get(data.getId());
				updateData.setItemCd(data.getItemCd());
				updateData.setItemName(data.getItemName());
				this.data = (Grid50000)drid50000Service.save(updateData);
			} else {
				// new case
				this.data = (Grid50000)drid50000Service.save(data);
			}
			setSuccess(true);
		} catch (Exception e) {
			setSuccess(false);
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	@Action(value = "delete", results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "criteria,data,list" }) })
	public String delete() {
		try {
			if(ids != null) {
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					drid50000Service.delete(Long.valueOf(split[i]));
				}
			}
			setSuccess(true);
		} catch (Exception e) {
			setSuccess(false);
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	public void validateSave() {
		if("000".equals(data.getItemCd())) {
			addFieldError("itemCd", "item cd can not be 000");
    		addActionError("itemCd");
			addActionMessage("item cd can not be 000");
		}
	}
	public DemoCriteriaVo getCriteria() {
		return criteria;
	}

	public void setCriteria(DemoCriteriaVo criteria) {
		this.criteria = criteria;
	}

	public Grid50000 getData() {
		return data;
	}

	public void setData(Grid50000 data) {
		this.data = data;
	}

	@JSON(name="rows")
	public List<Grid50000> getLists() {
		return lists;
	}

	public void setLists(List<Grid50000> lists) {
		this.lists = lists;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}